﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace USERS.CYT369.COM.Models
{
    public class UserSession
    {
        #region 初始
        private static IServiceProvider m_ServiceProvider;
        private Microsoft.AspNetCore.Http.HttpContext m_HttpContext;

        public static void Init(IServiceProvider serviceProvider)
        {
            m_ServiceProvider = serviceProvider;
        }

        public UserSession(Microsoft.AspNetCore.Http.HttpContext httpContext)
        {
            if (httpContext != null)
            {
                this.m_HttpContext = httpContext;
            }
        }

        public UserSession(Microsoft.AspNetCore.Http.IHttpContextAccessor httpContextAccessor)
        {
            if (httpContextAccessor != null && httpContextAccessor.HttpContext != null)
            {
                this.m_HttpContext = httpContextAccessor.HttpContext;
            }
        }

        public void Clear()
        {
            if (m_HttpContext.Session != null)
            {
                m_HttpContext.Session.Clear();
            }
        }

        public bool IsAuthenticated
        {
            get
            {
                if (m_HttpContext != null)
                {
                    if (m_HttpContext.User.Identity.IsAuthenticated)
                    {
                        if (this.SysUser != null)
                        {
                            return true;
                        }
                    }
                }

                return false;
            }
        }
        #endregion

        #region SysUser
        public USERS.CYT369.COM.MODELS.SysUser SysUser
        {
            get
            {
                if (m_HttpContext.Session != null)
                {
                    var buffer = new byte[0];
                    if (m_HttpContext.Session.TryGetValue(USERS.CYT369.COM.UTILITY.ConstStringClass.SysUser, out buffer))
                    {
                        try
                        {
                            return System.Text.Json.JsonSerializer.Deserialize<USERS.CYT369.COM.MODELS.SysUser>(System.Text.Encoding.UTF8.GetString(buffer));
                        }
                        catch { }
                    }
                }

                return null;
            }

            set
            {
                if (m_HttpContext.Session != null)
                {
                    m_HttpContext.Session.Set(USERS.CYT369.COM.UTILITY.ConstStringClass.SysUser, System.Text.Encoding.UTF8.GetBytes(System.Text.Json.JsonSerializer.Serialize<USERS.CYT369.COM.MODELS.SysUser>(value)));
                }
            }
        }

        public string FullName
        {
            get
            {
                var user = this.SysUser;
                if (user != null)
                {
                    return user.FullName;
                }

                return null;
            }
        }

        public string NickName
        {
            get
            {
                var user = this.SysUser;
                if (user != null)
                {
                    return user.NickName;
                }

                return null;
            }
        }

        public string UserName
        {
            get
            {
                var user = this.SysUser;
                if (user != null)
                {
                    return user.UserName;
                }

                return null;
            }
        }

        public int GetUserId()
        {
            var user = this.SysUser;
            if (user != null)
            {
                return user.Id;
            }

            return 0;
        }
        #endregion

        public string PhoneNumber
        {
            get
            {
                if (m_HttpContext.Session != null)
                {
                    var buffer = new byte[0];
                    if (m_HttpContext.Session.TryGetValue(USERS.CYT369.COM.UTILITY.ConstStringClass.PhoneNumber, out buffer))
                    {
                        return System.Text.Encoding.UTF8.GetString(buffer);
                    }
                }

                return null;
            }

            set
            {
                if (m_HttpContext.Session != null)
                {
                    m_HttpContext.Session.Set(USERS.CYT369.COM.UTILITY.ConstStringClass.PhoneNumber, System.Text.Encoding.UTF8.GetBytes(value));
                }
            }
        }

        /// <summary>
        /// 图片验证码
        /// </summary>
        public string ImageCaptchaCode
        {
            get
            {
                if (m_HttpContext.Session != null)
                {
                    var buffer = new byte[0];
                    if (m_HttpContext.Session.TryGetValue(USERS.CYT369.COM.UTILITY.ConstStringClass.ImageCaptchaCode, out buffer))
                    {
                        return System.Text.Encoding.UTF8.GetString(buffer);
                    }
                }

                return null;
            }

            set
            {
                if (m_HttpContext.Session != null)
                {
                    m_HttpContext.Session.Set(USERS.CYT369.COM.UTILITY.ConstStringClass.ImageCaptchaCode, System.Text.Encoding.UTF8.GetBytes(value));
                }
            }
        }

        /// <summary>
        /// Ticket Granting Ticket
        /// </summary>
        public string TGT
        {
            get
            {
                if(m_HttpContext.Session != null)
                {
                    var buffer = new byte[0];
                    if (m_HttpContext.Session.TryGetValue(USERS.CYT369.COM.UTILITY.ConstStringClass.TGT, out buffer))
                    {
                        return System.Text.Encoding.UTF8.GetString(buffer);
                    }
                }

                return null;
            }

            set
            {
                if (m_HttpContext.Session != null)
                {
                    m_HttpContext.Session.Set(USERS.CYT369.COM.UTILITY.ConstStringClass.TGT, System.Text.Encoding.UTF8.GetBytes(value));
                }
            }
        }


        #region 静态
        public static Microsoft.AspNetCore.Http.HttpContext GetHttpContext()
        {
            object factory = m_ServiceProvider.GetService(typeof(Microsoft.AspNetCore.Http.IHttpContextAccessor));
            var httpContext = ((Microsoft.AspNetCore.Http.IHttpContextAccessor)factory).HttpContext;

            return httpContext;
        }

        public static UserSession GetSession()
        {
            object factory = m_ServiceProvider.GetService(typeof(Microsoft.AspNetCore.Http.IHttpContextAccessor));
            var httpContext = ((Microsoft.AspNetCore.Http.IHttpContextAccessor)factory).HttpContext;

            return new UserSession(httpContext);
        }
        #endregion

    }
}
